package com.zeng.jiudian.dao.impl;

import com.zeng.jiudian.bean.HotelModel;
import com.zeng.jiudian.bean.HotelModelExample;
import com.zeng.jiudian.bean.HotelModelExample.Criteria;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.dao.HotelDao;
import com.zeng.jiudian.mapper.HotelMapper;
import com.zeng.jiudian.utils.MyBatisFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/28 - 9:51
 */
public class HotelDaoImpl implements HotelDao
{
    private SqlSession sqlSession;

    @Override
    public int add(HotelModel hotelModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        hotelModel.setCreateTime(dateFormat.format(new Date()));
        hotelModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            HotelMapper mapper = sqlSession.getMapper(HotelMapper.class);
            count = mapper.insert(hotelModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<HotelModel> findAll(HotelModel hotelModel)
    {
        List<HotelModel> list = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            HotelMapper mapper = sqlSession.getMapper(HotelMapper.class);
            HotelModelExample example = new HotelModelExample();
            Criteria criteria = example.createCriteria();

            if (!"".equals(hotelModel.getContent()))
            {
                criteria.andContentLike("%" + hotelModel.getContent() + "%");
            }
            if (!"".equals(hotelModel.getAddress()))
            {
                criteria.andAddressLike("%" + hotelModel.getAddress() + "%");
            }
            if (!"".equals(hotelModel.getPhone()))
            {
                criteria.andPhoneLike("%" + hotelModel.getPhone() + "%");
            }
            if (!"".equals(hotelModel.getFax()))
            {
                criteria.andFaxLike("%" + hotelModel.getFax() + "%");
            }
            if (!"".equals(hotelModel.getEmail()))
            {
                criteria.andEmailLike("%" + hotelModel.getEmail() + "%");
            }
            if (!"".equals(hotelModel.getWebsite()))
            {
                criteria.andWebsiteLike("%" + hotelModel.getWebsite() + "%");
            }
            if (!"".equals(hotelModel.getReservationPhone()))
            {
                criteria.andReservationPhoneLike("%" + hotelModel.getReservationPhone() + "%");
            }
            if (hotelModel.getIsShow() != null && hotelModel.getIsShow() >= 0)
            {
                criteria.andIsShowEqualTo(hotelModel.getIsShow());
            }
            if (hotelModel.getRecommend() != null && hotelModel.getRecommend() >= 0)
            {
                criteria.andIsShowEqualTo(hotelModel.getRecommend());
            }

            if (!"无".equals(hotelModel.getOrder()))
            {
                if ("创建时间".equals(hotelModel.getOrder()))
                {
                    if ("降序".equals(hotelModel.getLift()))
                    {
                        example.setOrderByClause("create_time desc");
                    }
                    else if ("升序".equals(hotelModel.getLift()))
                    {
                        example.setOrderByClause("create_time asc");
                    }
                }
                else if ("更新时间".equals(hotelModel.getOrder()))
                {
                    if ("降序".equals(hotelModel.getLift()))
                    {
                        example.setOrderByClause("update_time desc");
                    }
                    else if ("升序".equals(hotelModel.getLift()))
                    {
                        example.setOrderByClause("update_time asc");
                    }
                }
            }
            else
            {
                if ("降序".equals(hotelModel.getLift()))
                {
                    example.setOrderByClause("id desc");
                }
                else if ("升序".equals(hotelModel.getLift()))
                {
                    example.setOrderByClause("id asc");
                }
            }
            PageModel pageModel = hotelModel.getPageModel();
            if (pageModel != null)
            {
                Integer nowNum = pageModel.getNowNum();
                Integer pageSize = pageModel.getPageSize();
                if (nowNum != null && pageSize != 0 && pageSize != null)
                {
                    example.setStartRow((nowNum - 1) * pageSize);
                    example.setPageSize(pageSize);
                }
            }
            list = mapper.selectByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }

    @Override
    public int getCount(HotelModel hotelModel)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            HotelMapper mapper = sqlSession.getMapper(HotelMapper.class);
            HotelModelExample example = new HotelModelExample();
            Criteria criteria = example.createCriteria();

            if (!"".equals(hotelModel.getContent()))
            {
                criteria.andContentLike("%" + hotelModel.getContent() + "%");
            }
            if (!"".equals(hotelModel.getAddress()))
            {
                criteria.andAddressLike("%" + hotelModel.getAddress() + "%");
            }
            if (!"".equals(hotelModel.getPhone()))
            {
                criteria.andPhoneLike("%" + hotelModel.getPhone() + "%");
            }
            if (!"".equals(hotelModel.getFax()))
            {
                criteria.andFaxLike("%" + hotelModel.getFax() + "%");
            }
            if (!"".equals(hotelModel.getEmail()))
            {
                criteria.andEmailLike("%" + hotelModel.getEmail() + "%");
            }
            if (!"".equals(hotelModel.getWebsite()))
            {
                criteria.andWebsiteLike("%" + hotelModel.getWebsite() + "%");
            }
            if (!"".equals(hotelModel.getReservationPhone()))
            {
                criteria.andReservationPhoneLike("%" + hotelModel.getReservationPhone() + "%");
            }
            if (hotelModel.getIsShow() != null && hotelModel.getIsShow() >= 0)
            {
                criteria.andIsShowEqualTo(hotelModel.getIsShow());
            }
            if (hotelModel.getRecommend() != null && hotelModel.getRecommend() >= 0)
            {
                criteria.andIsShowEqualTo(hotelModel.getRecommend());
            }

            count = (int) mapper.countByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public int delById(int id)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            HotelMapper mapper = sqlSession.getMapper(HotelMapper.class);
            count = mapper.deleteByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public HotelModel findById(int id)
    {
        HotelModel hotelModel = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            HotelMapper mapper = sqlSession.getMapper(HotelMapper.class);
            hotelModel = mapper.selectByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return hotelModel;
    }

    @Override
    public int update(HotelModel hotelModel)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            HotelMapper mapper = sqlSession.getMapper(HotelMapper.class);
            count = mapper.updateByPrimaryKeySelective(hotelModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public HotelModel getAll()
    {
        List<HotelModel> list = null;
        HotelModel model = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            HotelMapper mapper = sqlSession.getMapper(HotelMapper.class);
            list = mapper.selectAll();
            if (list != null)
            {
                model = list.get(0);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return model;
    }
}
